<h1>My referral link</h1>

<?=str_replace(array('&lt;','&gt;','{client_id}'),array('<','>',$user->id),$this->cms->fetchPageContent('account_aff_getlink'));?>

<?php
		$this->load->helper('form');
		if($afflink)
		{
		?>
<label>Resource link:</label><br />
<textarea cols="40" rows="5"><?=$afflink?></textarea>
<br />
<a href="<?=current_url()?>">Back</a>
		<?php
		}
		else
		{
		?>

<p>Please select the link type and the language for your referral link:</p>
<?php echo form_open('account/afflink', array('id'=>'afflink-form')); ?>
    <table >
        <tr>
            <td>
                Resource type
            </td>
            <td>
                <select id="type" name="type" style="width: 100%">
					<?php
							$links = new Ref_link();
							$links->order_by('id', 'asc')->get();
							foreach($links as $link)
							{
									if($link->ib=='yes' && $this->session->userdata('ib'))
									{
											echo "\n\t\t\t\t\t\t<option value=\"".$link->id."\">".$link->name."</option>";
									}
									elseif($link->ib=='no')
									{
											echo "\n\t\t\t\t\t\t<option value=\"".$link->id."\">".$link->name."</option>";
									}
							}
					?>
					<option value="-1">Custom</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                Resource language
            </td>
            <td>
                <select name="lang" style="width: 100%;">

                    <option value="en">English</option>
                    <option value="fr">French</option>
                </select>
            </td>
        </tr>
		<tr id="custom_url-row" style="display:none;">
            <td>
                Page URL
            </td>
            <td>
                <input type="text" size="32" id="custom_url" name="custom_url" value="" />
            </td>
        </tr>
        <tr>
        	<td>&nbsp;</td>
            <td>
                <input type="submit" value="Generate link" style="width: 100%;" />
            </td>
        </tr>
    </table>
<?=form_close()?>
<script language="">
				jQuery(document).ready(function(){
				
						// jQuery('#type').trigger('click');
						
						jQuery('#type').change(function(){
								if(jQuery(this).val()==-1) {
										jQuery('#custom_url-row').show();
								} else {
										jQuery('#custom_url-row').hide();
								}
						});
						
						jQuery('#afflink-form').submit(function(){
								if(jQuery('#type').val()==-1) {
										if( !jQuery('#custom_url').val() )
										{
												alert("Please provide URL");
												jQuery('#custom_url').focus();
												return false;
										} 
										
										var cus_url = jQuery('#custom_url').val().trim().toLowerCase();
										if(cus_url.substr(0,7)!='http://')
										{
												cus_url = 'http://'+cus_url;
												jQuery('#custom_url').val(cus_url);
										}
										return true;
								} else {
										return true;
								}
						});
						
				});
		</script>


		<?php
		}
?>